#include <SpaceHUD.h>
#include <PlayerInterface.h>
#include <BaseObject.h>
#include <World.h>

#include <EnterMenu.h>
#include <UnloadArea.h>

#include <sstream>

SpaceHUD::SpaceHUD(World& gameworld)
:world(gameworld)
{
}

SpaceHUD::~SpaceHUD()
{
}

void SpaceHUD::Load()
{
	hudObjects.push_back( HUDObject( &world ) );
	hudObjects.push_back( HUDObject( &world ) );
	hudObjects.push_back( HUDObject( &world ) );
	hudObjects.push_back( HUDObject( &world ) );

	hudObjects[0].vertices = MakeBox(1,1);
	hudObjects[0].solid = 0;

	hudObjects[1].vertices = MakeBox(1,1);
	hudObjects[1].solid = 1;
	
	hudObjects[2].vertices = MakeBox(1,1);
	hudObjects[2].solid = 1;
	hudObjects[2].text = "X";
	hudObjects[2].textColor.Set(1,0,0);
	hudObjects[2].color.Set(0.5,0.5,0.5);
	hudObjects[2].bottomLeft = vec2(5,10);
	hudObjects[2].size = vec2(15,20);
	hudObjects[2].command = new EnterMenu(&world);
	
	hudObjects[3].bottomLeft = vec2(5,10);
	hudObjects[3].size = vec2(15,20);
	hudObjects[3].command = new UnloadArea(&world);
}

void SpaceHUD::Update()
{
	BaseObject* player = External::player->GetControlTarget();

	if( player )
	{
		{
			std::stringstream tostr;
			tostr << player->t.loc.x;
			tostr << ", ";
			tostr << player->t.loc.y;
			hudObjects[0].text = "Player: " + tostr.str();
			float newSize = hudObjects[0].text.size() * 9;
			hudObjects[0].size = vec2( ( newSize < hudObjects[0].size.x ? hudObjects[0].size.x : newSize ), 20 );
		}
		hudObjects[0].bottomLeft = vec2(0.05f * Global::GameWindow->GetW(), 0.95f * Global::GameWindow->GetH());

		
		if( player->prop.HasProperty("HP") )
		{
			std::stringstream tostr;
			tostr << (unsigned)player->prop.GetProperty("HP");
			hudObjects[1].hidden = 0;
			hudObjects[1].text = "HP: " + tostr.str();
			hudObjects[1].size = vec2( hudObjects[1].text.size() * 10, 20 );
		}
		else
			hudObjects[1].hidden = 1;
		
		hudObjects[1].bottomLeft = vec2(0.05f * Global::GameWindow->GetW(), 0.90f * Global::GameWindow->GetH());
	}

	for(unsigned i = 0; i < hudObjects.size(); ++i)
		hudObjects[i].Update();
}

void SpaceHUD::Draw()
{
	for(unsigned i = 0; i < hudObjects.size(); ++i)
		hudObjects[i].Draw();
}